package com.woniuxy.certified.web.controller;


import cn.hutool.core.bean.BeanUtil;
import com.woniuxy.certified.dao.CertifiedAccount;
import com.woniuxy.certified.dao.CertifiedInfo;
import com.woniuxy.certified.model.dto.CertifiedInfoDto;
import com.woniuxy.certified.model.form.LoadAllCertifiedForm;
import com.woniuxy.certified.model.form.LoadAllcertifiedinfoForm;
import com.woniuxy.certified.model.param.LoadAllCertifiedParam;
import com.woniuxy.certified.model.param.LoadAllcertifiedinfoParam;
import com.woniuxy.certified.model.param.UpdateChargePriceParam;
import com.woniuxy.certified.model.param.UpdateStallPriceParam;
import com.woniuxy.certified.service.CertifiedInfoService;
import com.woniuxy.core.model.Result;
import com.woniuxy.core.util.JwtUtil;
import com.woniuxy.core.util.PageInfo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiOperation;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author 凌玉春
 * @since 2024年01月15日
 */
@RestController
@RequestMapping("/certifiedInfo")
@Api(tags = "物业详情接口")
public class CertifiedInfoController {


    @Resource
    private CertifiedInfoService certifiedInfoService;

    @ApiOperation("平台加载所有物业信息")
    @PostMapping("/loadAllCertified")
    public Result<PageInfo> loadAllCertified(@RequestBody LoadAllCertifiedForm laf){
        LoadAllCertifiedParam param = BeanUtil.toBean(laf, LoadAllCertifiedParam.class);
        PageInfo<CertifiedInfoDto> pageInfo = certifiedInfoService.loadAllCertified(param);
        return Result.getResult(PageInfo.class).setMessage("查询所有物业信息成功").setData(pageInfo);
    }

    @ApiOperation("根据物业账号id查找物业")
    @GetMapping("/findCertifiedAccountById")
    @ApiImplicitParam(name = "certifiedInfoId",value = "物业id")
    public Result<CertifiedAccount> findCertifiedAccountById(Integer certifiedInfoId){
        System.out.println(certifiedInfoId);
        CertifiedAccount certifiedAccount=certifiedInfoService.findCertifiedAccountById(certifiedInfoId);
        return Result.getResult(CertifiedAccount.class).setMessage("根据id查找物业信息成功").setData(certifiedAccount);
    }

    @ApiOperation("根据物业id查找物业")
    @GetMapping("/findCertifiedById")
    @ApiImplicitParam(name = "certifiedInfoId",value = "物业id")
    public Result<CertifiedInfo> findCertifiedById(Integer certifiedInfoId){
        CertifiedInfo certified=certifiedInfoService.findCertifiedById(certifiedInfoId);
        return Result.getResult(CertifiedInfo.class).setMessage("根据id查找物业信息成功").setData(certified);
    }


    @ApiOperation("物业管理员修改普通车位单价")
    @GetMapping("/updateStallPrice")
    @ApiImplicitParam(name = "price",value = "普通车位价格")
    public Result updateStallPrice(Double price,@RequestHeader String token){
        String certifiedInfoId = JwtUtil.parseAdminToken(token).get("certifiedInfoId").toString();
        UpdateStallPriceParam param = new UpdateStallPriceParam();
        param.setPrice(price);
        param.setCertifiedInfoId(Integer.valueOf(certifiedInfoId));
        certifiedInfoService.updateStallPrice(param);
        return Result.getResult().setMessage("修改车位单价成功");
    }


    @ApiOperation("物业管理员修改电车车位单价")
    @GetMapping("/updateChargePrice")
    @ApiImplicitParam(name = "price",value = "电车车位价格")
    public Result updateChargePrice(Double price,@RequestHeader String token){
        String certifiedInfoId = JwtUtil.parseAdminToken(token).get("certifiedInfoId").toString();
        UpdateChargePriceParam param = new UpdateChargePriceParam();
        param.setPrice(price);
        param.setCertifiedInfoId(Integer.valueOf(certifiedInfoId));
        certifiedInfoService.updateChargePrice(param);
        return Result.getResult().setMessage("修改电车位单价成功");
    }


    @ApiOperation("用户分页查询所有物业信息")
    @GetMapping("/loadAllcertifiedinfo")
    public Result<PageInfo> loadAllcertifiedinfo(LoadAllcertifiedinfoForm laf){
        LoadAllcertifiedinfoParam param = BeanUtil.toBean(laf, LoadAllcertifiedinfoParam.class);
        PageInfo<CertifiedInfo> pageInfo = certifiedInfoService.loadAllcertifiedinfo(param);
        return Result.getResult(PageInfo.class).setMessage("分页查询所有物业信息成功").setData(pageInfo);
    }

}

